package com.wingscode.common.utils;

import com.wingscode.modules.common.entity.AgentEntity;
import com.wingscode.modules.common.entity.ServiceUsageRecordEntity;
import com.wingscode.modules.common.service.AgentService;
import com.wingscode.modules.common.service.ServiceUsageRecordService;
import com.wingscode.modules.sys.entity.InquiryEntity;
import com.wingscode.modules.sys.entity.OfferPriceEntity;
import com.wingscode.modules.sys.service.InquiryService;
import com.wingscode.modules.sys.service.OfferPriceService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class TaskUtils {

    @Autowired
    private InquiryService inquiryService;
    @Autowired
    private OfferPriceService offerPriceService;
    @Autowired
    private AgentService agentService;
    @Autowired
    private ServiceUsageRecordService serviceUsageRecordService;

    @ApiOperation("发布信息48小时后失效")
    @Scheduled(cron = "0 0 0 * * ?")
    public void doInquiryInvalid() {
        // 获得已失效的询价信息
        List<InquiryEntity> inquiryList = inquiryService.invalidList();
        if (inquiryList != null && inquiryList.size() > 0) {
            for (InquiryEntity entity : inquiryList) {
                // 更新状态为已失效
                entity.setState(2);
                entity.setUpdateTime(new Date());

                inquiryService.updateById(entity);

                // 更新当前询价的报价信息为已失效
                OfferPriceEntity offerPrice = new OfferPriceEntity();
                offerPrice.setInquiryId(entity.getId());
                offerPrice.setState(3);
                offerPrice.setUpdateTime(new Date());

                offerPriceService.updateByInquiryId(offerPrice);

                // 发布次数+1
                AgentEntity agent = agentService.getById(entity.getAgentId());
                if (agent != null) {
                    if (entity.getType() == 4)
                        // 人才猎聘次数+1
                        agent.setPersonnelNum(agent.getPersonnelNum() + 1);
                    else
                        // 企业资质次数+1
                        agent.setQualNum(agent.getQualNum() + 1);
                    agent.setUpdateTime(new Date());

                    agentService.updateById(agent);

                    // 添加增值服务使用记录信息
                    ServiceUsageRecordEntity usageRecord = new ServiceUsageRecordEntity();
                    usageRecord.setAgentId(entity.getAgentId());
                    usageRecord.setUserId(entity.getUserId());
                    usageRecord.setCreateTime(new Date());
                    if (entity.getType() == 4)
                        usageRecord.setTitle("人才猎聘信息退回");
                    else
                        usageRecord.setTitle("企业资质信息退回");
                    usageRecord.setNum(1);
                    usageRecord.setType(1);

                    serviceUsageRecordService.save(usageRecord);
                }
            }
        }
    }
}
